begintownscript;

variables;
short bmessage,rctx,rcty,pcs,others_ok,twondx;
string custom_m,custom_m2;

body;

beginstate 0;

	if(get_flag(99,4) == 1){
		if(get_flag(99,3) == 0)
			split_off_one_char(32,4,0,0);
		if(get_flag(99,3) == 1)
			split_off_one_char(59,32,0,0);
		if(get_flag(99,3) == 2)
			split_off_one_char(4,32,0,0);
		if(get_flag(99,3) == 3)
			split_off_one_char(32,59,0,0);
		add_char_to_party(20);
		if(get_flag(99,3) == 0){
			march_party(32,5);
			force_view_center(32,5);
			set_character_facing(4,4);
		}
		if(get_flag(99,3) == 1){
			march_party(58,32);
			force_view_center(58,32);
			set_character_facing(4,2);
		}
		if(get_flag(99,3) == 2){
			march_party(5,32);
			force_view_center(5,32);
			set_character_facing(4,6);
		}
		if(get_flag(99,3) == 3){
			march_party(32,58);
			force_view_center(32,58);
			set_character_facing(4,0);
		}
		force_instant_terrain_redraw();
		block_entry(1);
	}
	else{
		erase_char(20);
	}

	set_crime_tolerance(5);

	set_name(6,"Bladesman Janet");
	set_name(7,"Bladesman Ivy");
	set_name(8,"Hunter");
	set_name(9,"Bladesman Acacia");
	set_name(10,"Bladesman Lloyd");
	set_name(11,"Bladesman Gregor");
	set_name(12,"Bladesman Maxwell");
	set_name(13,"Isabel");
	set_name(14,"Bladesman Anthony");
	set_name(15,"Rufus");
	set_name(16,"Paolo");
	set_char_dialogue_pic(6,545,0);
	set_char_dialogue_pic(7,549,0);
	set_char_dialogue_pic(8,548,0);
	set_char_dialogue_pic(9,546,0);
	set_char_dialogue_pic(10,544,0);
	set_char_dialogue_pic(11,552,0);
	set_char_dialogue_pic(12,543,0);
	set_char_dialogue_pic(13,547,0);
	set_char_dialogue_pic(14,540,0);
	set_char_dialogue_pic(15,551,0);
	set_char_dialogue_pic(16,550,0);
	set_char_dialogue_pic(17,541,0);

	if(get_flag(26,0) > 0)
		erase_char(17);

	if(get_flag(5,0) < 3){
		force_instant_terrain_redraw();
		message_dialog("A human!  So this fort still IS secure.  That's a relief.  The lone, noticeably alarmed guard immediately addresses you.","");
		force_view_center(54,32);
		force_instant_terrain_redraw();
		pause(10);
		if(party_size() > 1)
			message_dialog("_Pardon me, but you appear to be soldiers!  Do you have news of the reinforcements we were supposed to be receiving?_","You begin to respond, but then the soldier realizes that you have a long message.  _Pardon me, men.  Come closer to the fire if you must, I realize that it is cold outside._");
		if(party_size() == 1)
			message_dialog("_Pardon me, but you appear to be a soldier!  Do you have news of the reinforcements we were supposed to be receiving?_","You begin to respond, but then the soldier realizes that you have a long message.  _Pardon me, sir.  Come closer to the fire if you must, I realize that it is cold outside._");

		force_view_center(char_loc_x(0),char_loc_y(0));
		force_instant_terrain_redraw();

		set_flag(5,0,3);
	}

	if(get_flag(5,0) < 7){
		set_terrain(45,28,26);
		set_terrain(28,16,27);
		set_terrain(19,28,26);
		set_terrain(14,28,26);
	}

break;

beginstate 1;
break;

beginstate START_STATE;

	if(get_flag(5,0) >= 1 && get_flag(26,0) == 0){
		set_terrain(28,49,15);
	}

	if(get_crime_level() > 0){
		message_dialog("Angering the few survivors of Fort Ivalice is a profoundly bad idea.  For disobeying orders, you are given an on-the-spot court martial, allowing the short-handed fort to be taken even more readily.","Your death is not a pleasant one, but thanks to your unconsciousness, you are not able to notice it for very long.");
		kill_char(1000,3,0);
		end_scenario(0);
		end();
	}

break;

beginstate 10;

	if(get_flag(5,0) < 4){
		message_dialog("Anthony stops you.  _Pardon me.  I know you're anxious to get inside, but could we have a quick word first?_","");
		block_entry(1);
	}

break;

beginstate 11;

	if(get_flag(5,0) < 6){
		message_dialog("You haven't gauged the situation here.  While being here is not exactly safe for your health, being outside alone would be unmistakably worse.","");
		block_entry(1);
	}

break;

beginstate 12;

	if(is_combat() == 1){
		block_entry(1);
		end();
	}
	if(get_flag(5,0) < 5){
		message_dialog("As you approach the campfire, you notice that there aren't many soldiers in sight.  The situation seems pretty desperate if they've been reduced to this few numbers, nevermind the fact that they have no spellcasters.","There's one man in bright green armor sitting at the fire.  He seems to glow, and has eyes that don't miss a thing.  While most of the soldiers are either nervous or uptight, he seems to be completely at ease.  In charge.");
		set_flag(5,0,5);
	}

break;

beginstate 13;

	if(get_flag(25,0) == 0){
		message_dialog("This barrack is intensely unused, and is therefore almost unsanitary to be in.  The dust in the air that you kick up is palpable.  This room would be completely unfit for any sort of rest.","");
		set_flag(25,0,1);
	}

break;

beginstate 14;

	if(get_flag(26,0) == 1 && get_flag(99,7) == 0 && party_size() > 1){
		split_off_one_char(28,49,0,0);
		set_flag(99,7,1);
		set_flag(99,4,1);
		block_entry(1);
		force_view_center(28,49);
		set_character_facing(0,6);
		force_instant_terrain_redraw();
	}
	if(party_size() == 1){
		relocate_character(0,28,49);
		set_flag(99,7,1);
		block_entry(1);
		force_view_center(28,49);
		set_character_facing(0,6);
		force_instant_terrain_redraw();
	}

	if(get_flag(23,0) > 7 && get_flag(26,0) == 0){
		reset_dialog();
		add_dialog_str(0,"This barrack has clean beds and a living light source.  If you wanted, you could sleep here.  It would be a very well-earned rest, to say the least!",0);
		if(party_size() > 1){
			add_dialog_choice(0,"Nah, we'll go to bed later.");
			add_dialog_choice(1,"Yeah, we'll have a nap now.");
		}
		if(party_size() == 1){
			add_dialog_choice(0,"Nah, I'll go to bed later.");
			add_dialog_choice(1,"Yeah, I'll have a nap now.");
		}
		bmessage = run_dialog(0);
		if(bmessage == 1){
			block_entry(1);
			end();
		}

		rctx = 27;
		while(rctx >= 23){
			march_party(rctx,49);
			force_instant_terrain_redraw();
			pause(5);

			if((party_size() + rctx) == 28){
				set_terrain(28,49,11);
				force_instant_terrain_redraw();
				play_sound(59);
				pause(5);
			}

			rctx = (rctx - 1);
		}

		if(get_flag(99,0) == 1)
			message_dialog("Chika...  Stand with me, ever in good stead.","");
		if(get_flag(99,0) == 2)
			message_dialog("Chika...  I did good today, right?","");

		force_view_center(1,1);
		force_instant_terrain_redraw();
		rcty = 49;
		while(rcty <= 52){
			march_party(22,rcty);
			rcty = (rcty + 1);
		}
		activate_hidden_group(1);
		erase_char(17);
		set_ticks_forward(620);
		play_sound(20);
		pause(15);

		set_terrain(28,49,15);
		play_sound(58);
		if(party_size() > 1)
			message_dialog("_Soldiers!  Wake up.  Come on, you sleepy heads!  It's morning, and we have work to do.  Up-up!  Up and at 'em!_","");
		if(party_size() == 1)
			message_dialog("_Hey!  Wake up.  Come on, you sleepy head!  It's morning, and we have work to do.  Up-up!  Rise and at 'em!_","");

		force_view_center(24,49);
		force_instant_terrain_redraw();
		pause(10);
		reset_dialog();
		add_dialog_str(0,"It's the Dervish...  He's smiling, but this doesn't change the fact that you only got roughly four hours of sleep.  He's probably used to this, though...",0);
		if(get_flag(99,0) == 1){
			add_dialog_str(1,"On the other hand, you got more and better rest than anyone else here.  Besides, you didn't see Malachai getting any winks in.  He gave you a luxury.",0);
		}
		if(get_flag(99,0) == 2){
			add_dialog_str(1,"But really, he only let you get this little sleep after fighting so many nephils!  He's a nice guy, but a bit of a hardass.  On the other hand, he IS a Dervish...",0);
		}
		clear_buffer();
		append_string("_Good morning, ");
		pcs = 0;
		while(pcs <= 3){
			if(char_ok(pcs) == TRUE){
				others_ok = (pcs + 1);
				twondx = 0;
				while(others_ok < 4 && twondx < 2){
					if(char_ok(others_ok) == 1){
						twondx = (twondx + 1);
					}
					others_ok = (others_ok + 1);
				}
				if(twondx == 1){
					append_char_name(pcs);
					append_string(", and ");
				}
				if(twondx > 1){
					append_char_name(pcs);
					append_string(", ");
				}
				if(twondx == 0){
					append_char_name(pcs);
					append_string(".  ");
				}
			}
			pcs = (pcs + 1);
		}
		append_string("I see that you are ");
		if(party_size() > 1)
			append_string("all ");
		append_string("awake.  I apologize for not letting you sleep longer, but since the Ratbane Revival is not slowing down, we too must be active._");
		get_buffer_text(custom_m);

		add_dialog_str(2,custom_m,0);
		add_dialog_str(3,"_Besides, I have a plan for today!  It will be risky, but if we succeed, then we'll have made a very significant gain on the nephil army._",0);
		clear_buffer();
		append_string("_");
		append_char_name(0);
		append_string(", I want you to speak with me.  ");
		if(party_size() > 2)
			append_string("  Your partners can stay here for today and guard the fort.  I don't want to leave it too under-defended.  ");
		if(party_size() == 2)
			append_string("Your partner can stay here for today and guard the fort.  I don't want to leave it too under-defended.  ");
		append_string("This morning will be a perfect time to get this job done._");
		get_buffer_text(custom_m2);
		add_dialog_str(4,custom_m2,0);
		add_dialog_choice(0,"Yes sir.");
		bmessage = run_dialog(1);

		relocate_character(19,32,49);
		force_view_center(char_loc_x(0),char_loc_y(0));
		set_terrain(28,49,11);
		force_instant_terrain_redraw();

		block_entry(1);
		set_flag(26,0,1);
		set_flag(99,6,1);
	}

break;

beginstate 15;

	if(get_flag(26,0) == 1){
		message_dialog("The Dervish is standing patiently, waiting for you to wipe the morning out of your eyes and speak to him.  You should do so post-haste.","");
		block_entry(1);
	}

break;

beginstate 20;

	set_flag(99,3,0);
	if(get_flag(38,0) == 0 && get_flag(99,4) == 1){
		message_dialog("Your party is now effectively 1PC while outdoors.  However, due to the game engine, four PCs will be displayed, and Error messages will be displayed whenever you leave town.","These messages will not affect gameplay; for plot and gameplay purposes, you do not have your other PCs.  Thank you for your understanding.");
		set_flag(38,0,1);
	}

break;

beginstate 21;

	set_flag(99,3,1);
	if(get_flag(38,0) == 0 && get_flag(99,4) == 1){
		message_dialog("Your party is now effectively 1PC while outdoors.  However, due to the game engine, four PCs will be displayed, and Error messages will be displayed whenever you leave town.","These messages will not affect gameplay; for plot and gameplay purposes, you do not have your other PCs.  Thank you for your understanding.");
		set_flag(38,0,1);
	}

break;

beginstate 22;

	set_flag(99,3,2);
	if(get_flag(38,0) == 0 && get_flag(99,4) == 1){
		message_dialog("Your party is now effectively 1PC while outdoors.  However, due to the game engine, four PCs will be displayed, and Error messages will be displayed whenever you leave town.","These messages will not affect gameplay; for plot and gameplay purposes, you do not have your other PCs.  Thank you for your understanding.");
		set_flag(38,0,1);
	}

break;

beginstate 23;

	set_flag(99,3,3);
	if(get_flag(38,0) == 0 && get_flag(99,4) == 1){
		message_dialog("Your party is now effectively 1PC while outdoors.  However, due to the game engine, four PCs will be displayed, and Error messages will be displayed whenever you leave town.","These messages will not affect gameplay; for plot and gameplay purposes, you do not have your other PCs.  Thank you for your understanding.");
		set_flag(38,0,1);
	}

break;

beginstate 24;

	if(get_height(char_loc_x(0),char_loc_y(0)) > 4){
		if(char_loc_x(0) != 47 || char_loc_y(0) != 50){
			block_entry(1);
		}
	}

break;

beginstate 25;

	if(get_height(char_loc_x(0),char_loc_y(0)) > 4){
		if(char_loc_x(0) != 47 || char_loc_y(0) != 56){
			block_entry(1);
		}
	}

break;

beginstate 26;

	move_to_new_town(3,7,55);

break;
